package org.shop.weixin.util;



import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import org.shop.weixin.util.GetWxOrderno;
import org.shop.weixin.util.RequestHandler;
import org.shop.weixin.util.TenpayUtil;

public class WxBillTool {

	public static Map orderQueryV(String out_trade_no) {
		String appid = "wx683b4884ae54010e";
		String appsecret = "be364e45d72ae240acd0ceec4e51fa89";
		String mch_id = "1275832201";//邮件里给的
		String pkey = "hD77Y8BTvnoVvIb9kkJrMgOwjm9Xv331";//商户平台里自己设的密钥
		String url="https://api.mch.weixin.qq.com/pay/orderquery";
		String currTime = TenpayUtil.getCurrTime();
		// 8位日期
		String strTime = currTime.substring(8, currTime.length());
		// 四位随机数
		String strRandom = TenpayUtil.buildRandom(4) + "";
		// 10位序列号,可以自行调整。
		String nonce_str = strTime + strRandom;
		Map map=new HashMap();
		SortedMap<String, String> packageParams = new TreeMap<String, String>();
		packageParams.put("appid", appid);
		packageParams.put("mch_id", mch_id);
		packageParams.put("nonce_str", nonce_str);
		packageParams.put("out_trade_no", out_trade_no);

		RequestHandler reqHandler = new RequestHandler(
				null, null);
		reqHandler.init(appid, appsecret, pkey);
		String sign = reqHandler.createSign(packageParams);
		String xmlParam = "<xml>" + "<appid>" + appid + "</appid>" + "<mch_id>"
				+ mch_id + "</mch_id>" + "<nonce_str>" + nonce_str
				+ "</nonce_str>" + "<sign><![CDATA[" + sign + "]]></sign>"
				+ "<out_trade_no>" + out_trade_no + "</out_trade_no>"
				+ "</xml>";
//		log.info(xmlParam);
			map=GetWxOrderno.doXML(url, xmlParam);

			return map;
	}

	/**
	 * 下载微信支付对账单
	 * @param args
	 * @return
	 */
	public static Map orderDown(String billDate) {
		String appid = "wx683b4884ae54010e";
		String appsecret = "be364e45d72ae240acd0ceec4e51fa89";
		String mch_id = "1275832201";//邮件里给的
		String pkey = "hD77Y8BTvnoVvIb9kkJrMgOwjm9Xv331";//商户平台里自己设的密钥
		String url="https://api.mch.weixin.qq.com/pay/downloadbill";
		String currTime = TenpayUtil.getCurrTime();
		// 8位日期
		String strTime = currTime.substring(8, currTime.length());
		// 四位随机数
		String strRandom = TenpayUtil.buildRandom(4) + "";
		// 10位序列号,可以自行调整。
		String nonce_str = strTime + strRandom;
		SortedMap<String, String> packageParams = new TreeMap<String, String>();
		packageParams.put("appid", appid);
		packageParams.put("mch_id", mch_id);
		packageParams.put("bill_type", "ALL");
		packageParams.put("nonce_str", nonce_str);
		packageParams.put("bill_date", billDate);
		RequestHandler reqHandler = new RequestHandler(
				null, null);
		reqHandler.init(appid, appsecret, pkey);
		String sign = reqHandler.createSign(packageParams);
		String xmlParam = "<xml>" + "<appid>" + appid + "</appid>" + "<mch_id>"+ mch_id + "</mch_id>"+"<bill_type>ALL</bill_type>"+ "<nonce_str>" + nonce_str
				+ "</nonce_str>"+"<bill_date>"+billDate+"</bill_date>" + "<sign><![CDATA[" + sign + "]]></sign>"
				+ "</xml>";

			return GetWxOrderno.doWxBillText(url, xmlParam);
	}
	public static void main(String[] args) {

/*OrderQuery.orderQueryV("2016070735457");*/

	/*	WxBillTool.orderDown("2016070735457");*/
		}
}
